package com.hqq.web.page;

import com.alibaba.fastjson.JSONObject;
import com.hqq.domain.WoocType;
import com.hqq.domain.WoocUser;
import com.hqq.domain.WoocVideo;
import com.hqq.domain.WoocVideoRemark;
import com.hqq.enums.ResultEnum;
import com.hqq.exceptions.InvokeException;
import com.hqq.service.WoocTypeService;
import com.hqq.service.WoocUserService;
import com.hqq.service.WoocVideoService;
import com.hqq.util.Response;
import com.hqq.web.consts.WoocConsts;
import com.hqq.web.util.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * IndexController
 * Created by heqianqian on 2018/3/24.
 */
@Controller
@RequestMapping("/index")
public class IndexController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private WoocTypeService woocTypeService;
    @Autowired
    private WoocVideoService woocVideoService;

    /*=============页面相关===========*/

    @RequestMapping("/index")
    public String index(Model model, HttpServletRequest request) {
        try {
            String userInfo = CookieUtil.findCookie(request, WoocConsts.COOKIE_KEY);
            if (StringUtils.isNotBlank(userInfo)) {
                WoocUser user = JSONObject.parseObject(userInfo, WoocUser.class);
                if (null != user) {
                    model.addAttribute("name", user.getUserName());
                }
            }
        } catch (Exception e) {
            logger.error("===IndexController index===", e);
            return "error";
        }
        return "index/index";
    }

    /*=============接口相关===========*/

    /**
     * 获取一级菜单列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_first_menu.ajax")
    public Response getFistMenu() {
        logger.debug("===IndexController getFistMenu===");
        Response response = null;
        try {
            List<WoocType> woocType = woocTypeService.listFirstType();
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), woocType);
        } catch (Exception e) {
            logger.error("===IndexController login===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 获取二级菜单列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_second_menu.ajax")
    public Response getSecondMenu(Long firstMenuId) {
        logger.debug("===IndexController getSecondMenu===firstMenuId:{}", firstMenuId);
        Response response = null;
        try {
            List<WoocType> woocType = woocTypeService.listSecondType(firstMenuId);
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), woocType);
        } catch (Exception e) {
            logger.error("===IndexController login===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 获取最热视频
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_hot_video.ajax")
    public Response getHotVideo() {
        logger.debug("===IndexController getHotVideo===");
        Response response = null;
        try {
            List<WoocVideo> videos = woocVideoService.getHotVideos();
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), videos.subList(0, 5));//展示5条 暂时不做分页
        } catch (Exception e) {
            logger.error("===IndexController login===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }

    /**
     * 获取最新视频
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/get_new_video.ajax")
    public Response getNewVideos() {
        logger.debug("===IndexController getNewVideos===");
        Response response = null;
        try {
            List<WoocVideo> videos = woocVideoService.getNewVideos();
            response = Response.from(ResultEnum.SUCCESS.toCode(), ResultEnum.SUCCESS.toDescription(), videos.subList(0, 5));
        } catch (Exception e) {
            logger.error("===IndexController login===", e);
            response = Response.from(ResultEnum.FAILED.toCode(), ResultEnum.FAILED.toDescription());
        }
        return response;
    }
}
